Option Explicit 
Dim xmlDoc,objFSO,objChordFile,objFolder,objInDir,objLogfile,objFileNameProcessedFile 
Dim strTitle,strAuthor,strCopyRight,strLyrics,strLine,strParsedLine,strWriteLine,strChordLine,strWordLine
Dim strInDirectory,strOutDirectory,strFileName,strFullPath,strLogDirectory
Dim arrLines
Dim iLines,iCnt
Dim bFoundChorus
'ForAppending = 8 ForReading = 1, ForWriting = 2
Const OpenMode = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set xmlDoc=CreateObject("Microsoft.XMLDOM")

strInDirectory = "Songs"
strOutDirectory = "ChordFiles"
strLogDirectory = "..\logs"

'Create Logfile 
If objFSO.FolderExists(strLogDirectory) Then
	'
Else
   Set objFolder = objFSO.CreateFolder(strLogDirectory)
   WScript.Echo "Just created " & (strLogDirectory)
End If



set objLogfile = objFSO.OpenTextFile(strLogDirectory & "\" & "convertOpensongLog.txt" , OpenMode, True)
objLogfile.WriteLine("Start Conversion Process")

set objFileNameProcessedFile = objFSO.OpenTextFile(strLogDirectory & "\" & "FilesProcessed.txt" , OpenMode, True)

' Check that the strOutDirectory folder exists
If objFSO.FolderExists(strOutDirectory) Then
   Set objFolder = objFSO.GetFolder(strOutDirectory)
   objLogfile.WriteLine("ChordPro Directory Exists. Name ==> " & strOutDirectory)
Else
   Set objFolder = objFSO.CreateFolder(strOutDirectory)
   objLogfile.WriteLine("Created ChordPro Directory. Name ==> " & strOutDirectory)
   WScript.Echo "Just created " & strOutDirectory
End If

set objInDir = objFSO.GetFolder(strInDirectory)
For each strFileName in objInDir.Files
objLogfile.WriteLine("Processing file " & strFileName.name)
objFileNameProcessedFile.writeLine(strFileName.name)
'wscript.echo  strFileName.name
'strFileName = "C:\Documents and Settings\wwelbyso\My Documents\Opensong\Above All"
strFullPath = strInDirectory & "\" & strFileName.name

'Set XML parms
xmlDoc.async="false"
xmlDoc.load(strFullPath)

'Get Elements
strTitle = xmlDoc.GetElementsByTagName("title").item(0).text
strAuthor = xmlDoc.GetElementsByTagName("author").item(0).text
'strCopyRight = xmlDoc.GetElementsByTagName("copyright").item(0).text
strLyrics = xmlDoc.GetElementsByTagName("lyrics").item(0).text

'Create file 

Set objChordFile = objFSO.OpenTextFile(strOutDirectory & "\" & strFileName.name & ".crd" , OpenMode, True)
objChordFile.WriteLine("{title:" & strTitle &"}")
'objChordFile.WriteLine(strAuthor)
'objChordFile.WriteLine(strCopyRight)
'objChordFile.WriteLine(strLyrics)

'Split the Lyrics into lines
arrLines = Split(strLyrics,vbLf)
iLines = UBound(arrLines)

'wscript.echo "Lines = " & iLines & "-"

'Step through the lines
iCnt = 0
bFoundChorus=false 
for iCnt=0 to iLines 
        'objLogfile.WriteLine(arrLines(iCnt))
        'objLogfile.WriteLine(arrLines(iCnt+1))
	If left(arrLines(iCnt),1) = "[" then 
	    If mid(arrLines(iCnt),2,1) = "C" then
		    bFoundChorus = true
		    objChordFile.WriteLine("{start_of_chorus}")
	    Else
	    	If bFoundChorus = true then 
	    		bFoundChorus = false 
	    		objChordFile.WriteLine("{end_of_chorus}")
	    	End If 
	    	objChordFile.WriteLine("")
	    End If
	Else
		
		If left(arrLines(iCnt),1) = "." then 
			 If iCnt = iLines then 
		        	wscript.echo strFileName.name + " Contains an Ending Code Line with no 'Words'"
		         End If 
			'Found some words and cords 
			'objChordFile.WriteLine(makeAChordLine(arrLines(iCnt),arrLines(iCnt+1)))
			strChordLine = makeAChordLine(rtrim(arrLines(iCnt)),rtrim(arrLines(iCnt+1)))
			iCnt = iCnt + 1 
			
		Else
         	   If left(arrLines(iCnt),1) = ";" then 
 		   '   Add a comment line 
 		        'objChordFile.WriteLine("")
			objChordFile.WriteLine("{comment:" & right(arrLines(iCnt),len(arrLines(iCnt))-1) & "}")
			
	         
		   Else 
			   If len(trim(arrLines(iCnt))) > 0 then 
				objChordFile.WriteLine(arrLines(iCnt))
			   'Else
			   '	objChordFile.WriteLine("")
			   End If 
		   end if
		End If 
	End If 


Next 

'just incase 
If bFoundChorus = true then 
	objChordFile.WriteLine("{end_of_chorus}")

End If 

objChordFile.Close

Next

wscript.echo "Completed converting files"

objLogfile.WriteLine("Completed converting files")

objLogfile.close 

'wscript.echo strTitle  
'wscript.echo strAuthor  
'wscript.echo strCopyRight  
'wscript.echo strLyrics  

Function makeAChordLine(chords,words)
Dim strRet,cLen,wLen,mLen,i,j,strTmp
Dim aChord,arrC
	strRet=""
	cLen = len(chords)
	wLen = len(words)
	mLen = cLen
	if wLen > cLen then 
		mLen = wLen
	End if 
'	objChordFile.WriteLine("**********START************")
'	objChordFile.WriteLine(chords)
	for i=1 to (mLen -1)
		'see if we have a cord 
		'wscript.echo chords & "-" & i & "-" 
		if mid(chords,i+1,1) <> " " and i < cLen then
			'read till we have a space
			aChord = " "
			arrC = split(right(chords,len(chords)-i))
			if uBound(arrC) >= 0 then 
				aChord = arrC(0)
			End if 
			j=i+len(aChord)-1
			strRet = strRet & "[" & aChord & "]"
			
			'objChordFile.WriteLine(strRet & "-" & i & "-" & j & "-" & uBound(arrC)) & "-" & mLen
			'write words
			strRet = strRet & mid(words,i+1,j-i+1)
			'move pointer on
			i = j
		else
			strRet = strRet & mid(words,i+1,1)
		end if 
		
	next
	'objChordFile.WriteLine(strRet & "-" & i & "-" & j & "-" & uBound(arrC)) & "-" & mLen
	'remove all underscores and spaces 
	j=1
	strTmp = ""
	for i=1 to len(strRet) 
		if mid(strRet,i,1) = "_" then 
			'skip
		else 
			strTmp = strTmp & mid(strRet,i,1)
		end if 
	next 
	strRet= strTmp
	
	objChordFile.WriteLine(strRet)
'	objChordFile.WriteLine("**********END************")
	
	makeAChordLine = strRet
End Function